package com.stgy.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stgy.lease.common.exception.StgyException;
import com.stgy.lease.common.result.ResultCodeEnum;
import com.stgy.lease.model.domain.FacilityInfo;
import com.stgy.lease.model.enums.FacilityInfoTypeEnum;
import com.stgy.lease.web.admin.service.FacilityInfoService;
import com.stgy.lease.web.admin.mapper.FacilityInfoMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lingx
 * @description 针对表【facility_info(配套信息表)】的数据库操作Service实现
 * @createDate 2024-08-28 11:00:29
 */
@Service
public class FacilityInfoServiceImpl extends ServiceImpl<FacilityInfoMapper, FacilityInfo>
        implements FacilityInfoService {

    //[根据类型]查询配套信息列表
    @Override
    public List<FacilityInfo> listByType(Integer type) {
        LambdaQueryWrapper<FacilityInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type != null, FacilityInfo::getType, type);
        return this.list(wrapper);
    }

    @Override
    public List<FacilityInfo> listByEnum(FacilityInfoTypeEnum typeEnum) {
        LambdaQueryWrapper<FacilityInfo> wrapper = new LambdaQueryWrapper<>();
        if (typeEnum != null) {
            wrapper.eq(FacilityInfo::getType, typeEnum.getCode());
        }
        return this.list(wrapper);
    }

    //新增或者更新配套信息业务
    @Override
    public boolean saveOrUpdateFacilityInfo(FacilityInfo facilityInfo) {
        if (facilityInfo == null || facilityInfo.getName() == null || facilityInfo.getIcon() == null) {
            throw new StgyException(ResultCodeEnum.DATA_EXISTS_ERROR);
        }
        LambdaQueryWrapper<FacilityInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FacilityInfo::getType, facilityInfo.getType().getCode());
        wrapper.eq(FacilityInfo::getName, facilityInfo.getName());
        FacilityInfo one = this.getOne(wrapper);
        //唯一校验 type+name 组合唯一
        if (one != null && !one.getId().equals(facilityInfo.getId())) {
//        if (one != null && one.getId().longValue() != facilityInfo.getId()) {
            throw new StgyException(ResultCodeEnum.DATA_EXISTS_ERROR);
        }

        return this.saveOrUpdate(facilityInfo);
    }
}



